package com.excemple.compose.ui.page.main

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.excemple.compose.ui.theme.AppTheme
import com.excemple.compose.ui.theme.Color_FF3700B3
import com.excemple.compose.ui.theme.Color_FFFFFFFF
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow

/**
 * 启动页
 * Created on 2022/5/5.
 * @author tdq
 */
@Composable
fun SplashPage(onNextPage: () -> Unit) {

    var mSecond by remember { mutableStateOf(3) }

    LaunchedEffect(key1 = null, block = {
        flow {
            for (i in 1..mSecond) {
                delay(1000)
                mSecond -= 1
            }
            emit(mSecond)
        }.collect {
            onNextPage.invoke()
        }
    })

    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(
                color = AppTheme.colors.themeUi
            ), contentAlignment = Alignment.BottomCenter
    ) {

        Column(
            verticalArrangement = Arrangement.SpaceBetween,
            horizontalAlignment = Alignment.CenterHorizontally,
            modifier = Modifier.fillMaxSize(),
        ) {
            Text(
                text = "Wan Android",
                fontSize = 32.sp,
                color = Color_FFFFFFFF,
                fontWeight = FontWeight.Bold,
                modifier = Modifier.padding(16.dp),
            )

            Text(
                text = "${mSecond}s",
                fontSize = 32.sp,
                color = Color_FF3700B3,
                fontWeight = FontWeight.Bold,
                modifier = Modifier
                    .padding(16.dp)
                    .clickable {
                    }
            )
        }

    }
}

@Preview
@Composable
fun PreviewSplashPage() {
    SplashPage {}
}